package com.want.controller.manager;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.want.api.blog.BlogApi;
import com.want.api.user.UserApi;
import com.want.model.dto.Result;
import com.want.req.base.PageReqDto;
import com.want.req.user.dto.RegisterReqDto;
import com.want.req.user.dto.UpdateUserReqDto;
import com.want.resp.base.PageResp;
import com.want.resp.blog.dto.BlogMetaDataNameAndCountRespDto;
import com.want.resp.blog.dto.BlogTagDto;
import com.want.resp.blog.dto.BlogTypeDto;
import com.want.resp.blog.dto.SimpleBlogMetaDataNameAndCountDto;
import com.want.resp.user.dto.RegisterRespDto;
import com.want.resp.user.dto.RoleDto;
import com.want.resp.user.dto.UserDto;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author WangZhiJian
 * @since 2021/12/25 0025
 */
@RestController
@RequestMapping("admin")
public class ManagerController {

    @Resource
    private BlogApi blogApi;
    @Resource
    private UserApi userApi;

    @ApiOperation(value = "获取一年内的文章贡献数和日期", notes = "获取一年内的文章贡献数和日期")
    @GetMapping("/getBlogContributeCountAndDates")
    public Mono<BlogMetaDataNameAndCountRespDto> getBlogContributeCountAndDates() {
        return blogApi.getBlogContributeCountAndDates();
    }

    @ApiOperation(value = "获取每个分类下文章数目", notes = "获取每个分类下文章数目")
    @GetMapping(value = "/getBlogCountByBlogSort")
    public Mono<List<SimpleBlogMetaDataNameAndCountDto>> getBlogCountByBlogSort() {
        return blogApi.getBlogCountByBlogSort();
    }


    @ApiOperation(value = "获取每个标签下文章数目", notes = "获取每个标签下文章数目", response = String.class)
    @GetMapping(value = "/getBlogCountByBlogTag")
    public Mono<List<SimpleBlogMetaDataNameAndCountDto>> getBlogCountByBlogTag() {
        return blogApi.getBlogCountByBlogTag();
    }

    @PostMapping("blogTag/add")
    @SaCheckRole(value = "admin")
    public Mono<Result<BlogTagDto>> addBlogTag(@RequestBody BlogTagDto blogTagDto){
        return blogApi.addBlogTag(blogTagDto);
    }

    @PostMapping("blogTag/delete")
    @SaCheckRole(value = "admin")
    public Mono<Result<List<Integer>>> deleteBlogTag(@RequestBody List<Integer> idList){
        return blogApi.deleteBlogTag(idList);
    }

    @PostMapping("blogTag/update")
    @SaCheckRole(value = "admin")
    public Mono<Result<BlogTagDto>> updateBlogTag(@RequestBody BlogTagDto blogTagDto){
        return blogApi.updateBlogTag(blogTagDto);
    }

    @PostMapping("blogType/add")
    @SaCheckRole(value = "admin")
    public Mono<Result<BlogTypeDto>> addBlogType(@RequestBody BlogTypeDto blogTypeDto){
        return blogApi.addBlogType(blogTypeDto);
    }
    @PostMapping("blogType/delete")
    @SaCheckRole(value = "admin")
    public Mono<Result<List<Integer>>> deleteBlogType(@RequestBody List<Integer> idList){
        return blogApi.deleteBlogType(idList);
    }
    @PostMapping("blogType/update")
    @SaCheckRole(value = "admin")
    public Mono<Result<BlogTypeDto>> updateBlogType(@RequestBody BlogTypeDto blogTypeDto){
        return blogApi.updateBlogType(blogTypeDto);
    }

    @PostMapping(value = "user/listUser")
    Mono<Result<PageResp<UserDto>>> listUser(@RequestBody PageReqDto pageReqDto){
        return userApi.listUser(pageReqDto);
    }
    @PostMapping(value = "user/add")
    @SaCheckRole(value = "admin")
    Mono<Result<RegisterRespDto>> addUser(@RequestBody RegisterReqDto registerReqDto){
        return userApi.saveUser(registerReqDto);
    }

    @GetMapping("role/roleList")
    Mono<Result<List<RoleDto>>> roleList(){
        return userApi.roleList();
    }
    @PostMapping("user/updateUser")
    @SaCheckRole(value = "admin")
    public Mono<Result<Integer>> updateUser(@RequestBody UpdateUserReqDto updateUserReqDto){
        return userApi.updateUser(updateUserReqDto);
    }
    @GetMapping("user/delete/{id}")
    @SaCheckRole(value = "admin")
    public Mono<Result<String>> delete(@PathVariable("id") Integer uid){
        return userApi.delete(uid);
    }

    @PostMapping("role/add")
    @SaCheckRole(value = "admin")
    public Mono<Result<RoleDto>> createRole(@RequestBody RoleDto roleDto){
        return userApi.createRole(roleDto);
    }

    @GetMapping("role/delete/{id}")
    @SaCheckRole(value = "admin")
    public Mono<Result<Integer>> deleteRole(@PathVariable("id") Integer id){
        return userApi.deleteRole(id);
    }
}
